package com.isoftstone.bank.controller.base;



import com.isoftstone.common.Page;
import com.isoftstone.common.entity.SysUser;
import com.isoftstone.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;

/**
 * 修改时间：2015、12、11
 */
public class BaseController {

    protected Logger logger = Logger.getLogger(this.getClass());

    private static final long serialVersionUID = 6357869213649815390L;

    public static String baseContextPath;
    public static String userContextPathFlag;
    public static String basePath;


    static {
                userContextPathFlag  = PropertiesUtils.getProperties("userContextPathFlag");
                baseContextPath = PropertiesUtils.getProperties("baseContextPath");
                basePath   = PropertiesUtils.getProperties("basePath");
      }


    /**
     * new PageData对象
     *
     * @return
     */
    public PageData getPageData()  {
            return new PageData(this.getRequest(),this.getResponse());
    }

    /**
     * 得到ModelAndView
     *
     * @return
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    /**
     * 得到request对象
     *
     * @return
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }



    /**
     * 得到request对象
     *
     * @return
     */
    public HttpServletResponse getResponse() {

        HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        return response;
    }


    /**
     * 得到32位的uuid
     *
     * @return
     */
    public String get32UUID() {
        return UuidUtil.get32UUID();
    }

    public int getintUUID() {
        return (int) (Math.random() * (10000000 - 100000) + 100000);
        //return UuidUtil.get32UUID();
    }

    /**
     * 得到分页列表的信息
     *
     * @return
     */
    public Page getPage() {
        return new Page();
    }

    public static void logBefore(Logger logger, String interfaceName) {
        logger.info("");
        logger.info("start");
        logger.info(interfaceName);
    }

    public static void logAfter(Logger logger) {
        logger.info("end");
        logger.info("");
    }

    @ExceptionHandler
    public ModelAndView exception(HttpServletRequest request, Exception ex) {
        logger.error("==============error start=============");
        ex.printStackTrace();
        ModelAndView mv = new ModelAndView("error");
        logger.error("exception", ex);
        logger.error("==============error end=============");
        mv.addObject("exception", ex.toString().replaceAll("\n", "<br/>"));
        mv.setViewName("500");
        return mv;

    }

    /**
     * 输出异常信息
     *
     * @param t
     * @return
     */
    public static String LogExceptionStack(Throwable t) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }

    protected Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }




    /**
     * 组装ajax分页对象
     * 
     * @return
     */
    protected Page getAjaxPage() {
        return new Page();
    }

    protected String getCurrentPage() throws Exception {
        PageData pd = new PageData(this.getRequest(),this.getResponse());

        String  pageindex = pd.getString("pageindex");

        if (StringUtils.isBlank(pageindex)) {
            pageindex="1";
        }
        return pageindex;
    }

    /**
     * Double相加
     * @param v1
     * @param v2
     * @return
     */
    public static Double add(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.add(b2).doubleValue());
    }

    public SysUser getUser() {
        return (SysUser) this.getSession().getAttribute(Const.SESSION_USER);
    }
}
